
var app = getApp();
var util = require('../../../../utils/util.js');
var status;  //上架-下架的输入值 0代表上架 1代表下架
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isxiajia: false, // 下架框
    isshangjia: false, // 上架框
    isdel: false,  // 删除框
    is_checked: {},//单选
    isChooseAll: false,   //全选
    isguige: [],  //规格显隐
    page: 1,   //当前页码
    count: 0,  //选择数量
    isopera: false,  //控制'操作'
    rightDatas:[],
    load: "搜索中",
    loading:1
  },
  // 点击 “操作” 
  operack: function () {
    this.setData({
      isopera: !this.data.isopera
    });
  },
  close_opera:function(){
    this.setData({
      isopera:false
    });
  },
  // 显示规格
  showspecs: function (e) {
    var that = this;
    var index = e.currentTarget.id;
    that.setData({
      isguige:[]
    });
    app.AppHttp('api/Supplier/getSpecs', {
      openid: app.globalData.openid,
      good_id: that.data.rightDatas[index].id
    }, function (res) {
      console.log(JSON.stringify(res.data));  
      that.setData({
        specs_list: res.data.specs_list,
        ["isguige[" + index + "]"]: true
      })  
    });
  },
  // 隐藏规格
  hidespecs: function (e) {
    var index = e.currentTarget.id
    this.setData({
      ["isguige[" + index + "]"]: false
    })
  },
  // 单选
  checkboxMerge: function (e) {
    return;
  },
  // 表单选择
  checkboxChange:function(e){
    var is_checked = {};
    for (var i = 0; i < e.detail.value.length; i++) {  
      is_checked[e.detail.value[i]] = true;
    };
    if (e.detail.value.length == this.data.rightDatas.length) {
      this.setData({
        isChooseAll: true,
        is_checkall: true,
        is_checked: is_checked,
        count: e.detail.value.length
      })
    } else {
      this.setData({
        isChooseAll: false,
        is_checkall: false,
        is_checked: is_checked,
        count: e.detail.value.length
      })
    }
  },
  // 全选
  chooseAll: function (e) {
    var is_checked={};
    if (!this.data.isChooseAll){
      for (var i = 0; i < this.data.rightDatas.length; i++) {
        is_checked[this.data.rightDatas[i].id] = true;     
      };
    };
    this.setData({
      isChooseAll: !this.data.isChooseAll,
      is_checkall: !this.data.isChooseAll,
      is_checked: is_checked,
      count: !this.data.isChooseAll == true ? this.data.rightDatas.length : 0
    })
  },
  // 表单提交
  formSubmit: function (e) {
    if (e.detail.target.dataset.status == 'move') {
      this.setData({
        ids: e.detail.value.shoptype
      })
      // console.log(this.data.ids);
      this.movechooseTap();
    } else if (e.detail.target.dataset.status == 'xiajia') {
      this.setData({
        ids: e.detail.value.shoptype
      })
      this.xiajia();
    } else if (e.detail.target.dataset.status == 'shangjia') {
      this.setData({
        ids: e.detail.value.shoptype
      })
      this.shangjia();
    } else if (e.detail.target.dataset.status == 'del') {
      this.setData({
        ids: e.detail.value.shoptype
      })
      this.del();
    }
  },
  // 点击移动
  movechooseTap: function () {
    var that = this;
    util.showBuyModal(this);
    that.setData({
      showModalStatus:true
    });
    app.AppHttp('api/Supplier/CateList', {
      openid: app.globalData.openid,
      id: app.globalData.loginInfo.id,
      e_type:'client',
      page: 1,
      limit: 20
    }, function (res) {
      // console.log(JSON.stringify(res.data))
      that.setData({
        typesmove: res.data.one_class_list,
        leftTypesmove: res.data.two_class_list,
        one_class: res.data.one_class_list[0].private_one_class,
        two_class: res.data.two_class_list[0].private_one_class,
        value:[0,0]
      });
    })
  },
  // 底部弹窗选择改变
  bindChange: function (e) {
    var that = this;
    var val = e.detail.value;
    var typesmove = that.data.typesmove;
    console.log(typesmove[val[0]].private_one_class);
    app.AppHttp('api/Supplier/CateList', {
      openid: app.globalData.openid,
      id: app.globalData.loginInfo.id,
      one_id: typesmove[val[0]].private_one_class,
      e_type:'client',
      page: 1,
      limit: 20
    }, function (res) {
      // console.log(JSON.stringify(res.data))
      if (val[1] > res.data.two_class_list.length - 1) {
        val[1] = res.data.two_class_list.length - 1
      }
      if (res.data.two_class_list.length == 0) {
        that.setData({
          leftTypesmove: [''],
          one_class: res.data.one_class_list[val[0]].private_one_class,
          two_class: -1
        })
        return;
      }
      that.setData({
        leftTypesmove: res.data.two_class_list,
        one_class: res.data.one_class_list[val[0]].private_one_class,
        two_class: res.data.two_class_list[val[1]].private_one_class
      });
    })
  },
  // 点击确定移动
  queding: function () {
    var that = this;
    console.log(that.data.ids);
    console.log(that.data.one_class);
    console.log(that.data.two_class);
    if (that.data.two_class == -1) {
      wx.showToast({
        title: '没有二级分类'
      })
      return;
    }
    app.AppHttp('api/Supplier/removeProduct', {
      ids: that.data.ids,
      one_class: that.data.one_class,
      two_class: that.data.two_class
    }, function (res) {
      // console.log(JSON.stringify(res.data))
        wx.showToast({
          title: res.data.msg
        }, 2000)
        if (res.data.status == 1) {
          that.setData({
            isopera: false,
            count: 0
          });
        }
    })
    util.hideBuyModal(that,"showModalStatus");
  },
  // 关闭底部移动弹窗
  movecancel: function () {
    util.hideBuyModal(this,"showModalStatus");
  },
  // 下架弹窗
  xiajia: function () {
    this.setData({
      isxiajia: true
    })
  },
  // 下架弹窗关闭
  xiacancel: function () {
    this.setData({
      isxiajia: false,
      isopera: false
    })
  },
  // 确定下架
  xiaconfirm: function () {
    status=1;
    var that = this;
    var rightDatas=this.data.rightDatas;
    var rightDatas = rightDatas.map(function(item) {
      if(that.data.is_checked[item.id]){
        item.status=0;    
      };
      return (item);     
    });
    this.display();
    this.setData({
      isxiajia: false,
      rightDatas: rightDatas,
      isopera: false,
      count: 0,
      is_checked: {},//单选
      isChooseAll: false, //全选
      is_checkall: false
    })
  },
  // 上架弹窗
  shangjia: function () {
    this.setData({
      isshangjia: true
    })
  },
  // 上架弹窗关闭
  shangcancel: function () {
    this.setData({
      isshangjia: false,
      isopera: false
    })
  },
  // 确定上架
  shangconfirm: function () {
    status = 0;
    var that = this;
    var rightDatas=this.data.rightDatas;
      var rightDatas = rightDatas.map(function(item) {
        if(that.data.is_checked[item.id]){
          item.status=1;    
        };
        return (item);     
      });
    this.display();
    this.setData({
      isshangjia: false,
      isopera: false,
      rightDatas: rightDatas,
      count: 0,
      is_checked: {},//单选
      isChooseAll: false, //全选
      is_checkall: false
    });
  },
  //  商品上架-下架共用接口
  display: function () {
    var that = this;
    app.AppHttp('api/Supplier/LowerShelf', {
      ids: that.data.ids,
      status: status
    }, function (res) {
      // console.log(JSON.stringify(res.data))
      if (res.data.status) {
        wx.showToast({
          title: res.data.msg
        })
      }
      console.log(JSON.stringify(res.data));
    })
  },
  // 删除弹窗
  del: function () {
    this.setData({
      isdel: true
    })
  },
  // 关闭删除弹窗
  delcancel: function () {
    this.setData({
      isdel: false,
      isopera: false
    })
  },
  // 确定删除弹窗
  delconfirm: function () {
    var that = this;
    app.AppHttp('api/Supplier/deleteGoods', {
      ids: that.data.ids
    }, function (res) {
      // console.log(JSON.stringify(res.data))
      if (res.data.status) {
        wx.showToast({
          title: res.data.msg
        });
        var rightDatas=that.data.rightDatas;   
        var rightDatas = rightDatas.filter(function (item) {  //数组过滤
          //元素值，元素的索引，原数组。
          return (!that.data.is_checked[item.id]);
        });
        that.setData({
          isdel: false,
          // page:1,
          // rightDatas:[],
          isdel:false,
          rightDatas:rightDatas,
          isopera: false,
          is_checked: {},//单选
          isChooseAll: false, //全选
          is_checkall: false,
          count: 0
        })
      };
    });
  },
  // 编辑产品
  editchanpin: function (e) {
    app.globalData.goodid = e.currentTarget.dataset.goodid;
    app.globalData.is_basic_search = e.currentTarget.dataset.is_basic_search;
    var index=e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../bianjichanpin/bianjichanpin_x?k_index='+index+"&nav=" + "sear"
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  // 输入商品名称
  sear:function(e){
    this.setData({
      shangpin: e.detail.value
    });
    app.globalData.task.abort();  //中断上一次搜索
    this.searck();
  },
  searconfirm:function(){
    this.searck();
  },
  searck:function(){  
    var that = this;
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    that.setData({
      rightDatas: [],
      page: 1,
      count:0,
      load:'搜索中',
      isChooseAll: false
    })
    console.log(that.data.rightDatas);
    this.upshoplist();
  },
  upshoplist: function (){
    if(this.data.page==1){
      wx.showLoading({
        title: this.data.load
      });
    };
    var that = this;
    var v = that.data.shangpin;
    app.AppHttp('api/Supplier/searchTemplateGoods', {
      id: app.globalData.loginInfo.id,
      page: that.data.page,
      limit: 20,
      keyword:v,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res.data));
      var rightDatas= that.data.rightDatas;
      app.globalData.shop_manage = 0;
      if (that.data.page==1){
        that.setData({
          loading:0,
          cur_page_index:0,
          rightDatas: res.data.list,
          allpage: res.data.page_count
        });
      }else{
        that.setData({
          loading:0,
          cur_page_index:rightDatas.length,
          rightDatas: that.data.rightDatas.concat(res.data.list),
          allpage: res.data.page_count
        });
      }
      console.log(that.data.page);
      console.log(that.data.allpage);
      wx.hideLoading();
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.shop_manage == 1){
      this.setData({
        count: 0,
        isChooseAll: false,
        bottomguige: false,
        load: "搜索中",
        rightDatas: [],
        page: 1
      })
      this.upshoplist();
    }   
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.page < that.data.allpage) {
      that.setData({
        page: that.data.page + 1,
        isChooseAll: false, //全选
        is_checkall: false,
        loading:1,
        load: '加载更多'
      });
      that.upshoplist();
    } else {
      this.setData({
        loading:0
      });
      wx.showToast({
        title: '没有了'
      })
    }
  }
})